package com.suitsoft.ut.web.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.entity.base.TeacherTemp;
import com.suitsoft.ut.service.base.TeacherTempManager;
import com.suitsoft.ut.util.StringUtils;

/**
 * 确认 教师导入数据
 *@author Suine
 */
@SuppressWarnings("serial")
public class TeacherImpConfirmAction extends ActionSupport{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private TeacherTempManager teacherTempManager;
	
	private List<Map<String,Object>> resutlist;
	private Map<String,Object> totail;
	private String college;
	private List<TeacherTemp> teacherList;
	/** 被选ID */
	private List<Long> checkIds;
	
	@Override
	public String execute() throws Exception {
		resutlist = teacherTempManager.getImpList();
		if(resutlist != null && !resutlist.isEmpty()){
			totail = new HashMap<String,Object>();
			int teacherCnt = 0;
			int collegeCnt = resutlist.size();
			int d = 0;
			int nd =0;
			for(Map<String,Object> m : resutlist){
				teacherCnt += Integer.valueOf(m.get("C") == null ? "0" : m.get("C").toString());
				d += Integer.valueOf(m.get("D") == null ? "0" : m.get("D").toString());
				nd += Integer.valueOf(m.get("ND") == null ? "0" : m.get("ND").toString());
			}
			totail.put("collegeCnt", collegeCnt);
			totail.put("teacherCnt", teacherCnt);
			totail.put("d", d);
			totail.put("nd", nd);
		}
		return SUCCESS;
	}
	
	@Override
	public String input(){
		try {
			if(StringUtils.isEmpty(college)){
				addActionMessage("非法操作");
				return SUCCESS;
			}
			teacherList = teacherTempManager.findBy("college", college);
			if(teacherList == null ||teacherList.isEmpty()){
				addActionMessage("未查到信息！");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败");
			return SUCCESS;
		}
		return INPUT;
	}

	@LogAnnotation(log = "基础数据导入 - 教师 - 确认学生数据 - 保存")
	public String save(){
		if(checkIds == null || checkIds.isEmpty()){
			addActionMessage("请选择教师之后，再提交 ");
			return input();
		}
		try {
			teacherList = teacherTempManager.get(checkIds);
			teacherTempManager.saveData(teacherList);
			addActionMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败");
		}
		return SUCCESS;
	}
	
	@Autowired
	public void setTeacherTempManager(TeacherTempManager teacherTempManager) {
		this.teacherTempManager = teacherTempManager;
	}
	public List<Map<String, Object>> getResutlist() {
		return resutlist;
	}
	public void setResutlist(List<Map<String, Object>> resutlist) {
		this.resutlist = resutlist;
	}
	public Map<String, Object> getTotail() {
		return totail;
	}
	public void setTotail(Map<String, Object> totail) {
		this.totail = totail;
	}
	public List<TeacherTemp> getTeacherList() {
		return teacherList;
	}
	public void setTeacherList(List<TeacherTemp> teacherList) {
		this.teacherList = teacherList;
	}
	public String getCollege() {
		return college;
	}
	public void setCollege(String college) {
		this.college = college;
	}
	public List<Long> getCheckIds() {
		return checkIds;
	}
	public void setCheckIds(List<Long> checkIds) {
		this.checkIds = checkIds;
	}
}

